#include "Interval.h"

const Interval Interval::empty    = Interval(+std::numeric_limits<double>::infinity(), -std::numeric_limits<double>::infinity());
const Interval Interval::universe = Interval(-std::numeric_limits<double>::infinity(), +std::numeric_limits<double>::infinity());

Interval::Interval(const Interval&a, const Interval&b)
{
	min = a.min <= b.min ? a.min : b.min;
	max = a.max >= b.max ? a.max : b.max;
}

Interval Interval::expand(double delta) const
{
	auto padding = delta / 2;
	return Interval(min - padding, max + padding);
}

double Interval::clamp(double x) const
{
	if (x < min) return min;
	if (x > max) return max;
	return  x;
}

Interval operator+(const Interval& ival, double displacement)
{
	return Interval(ival.min + displacement, ival.max + displacement);
}

Interval operator+(double displacement, const Interval& ival)
{
	return ival + displacement;
}
